﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms ;

namespace MetaNote.Controls
{
    public class SimpleTextBox : TextBox
    {
        private Stack<string> undoStack = new Stack<string>();
        private Stack<string> redoStack = new Stack<string>();
        private bool UndoInProgress = false;
        private bool RedoInProgress = false;

        public void Redo()
        {
            if (redoStack.Count <= 0) return;
            RedoInProgress = true;
            ////redoStack.Push(txt.Text)
            this.Text = redoStack.Pop();
            Application.DoEvents();
            RedoInProgress = false;
        }

        public new void Undo()
        {
            //base.Undo();
            
            if (undoStack.Count <= 0) return;
            UndoInProgress = true;
            redoStack.Push(this.Text);
            this.Text = undoStack.Pop();
            Application.DoEvents();
            UndoInProgress = false;
        }

        protected override void OnTextChanged(System.EventArgs e)
        {
            base.OnTextChanged(e);
            if ((!UndoInProgress))
            {
                undoStack.Push(this.Text);
                if (!RedoInProgress)
                {
                    redoStack.Clear();
                }
            }
        }

        public new bool CanUndo
        {
            get { return (undoStack.Count > 0); }
        }

        public bool CanRedo
        {
            get { return (redoStack.Count > 0); }
        }
  

    }
}
